﻿using CMS.Models;
using CMS.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class CargoTypeViewModel : ViewModelBase
    {
        private List<CargoType> cargoTypes = new List<CargoType>();
        public List<CargoType> CargoTypes
        {
            get { return cargoTypes; }
            set
            {
                cargoTypes = value;
                RaisePropertyChanged();
            }
        }

        public CargoTypeViewModel()
        {
            cargoTypes = new CargoTypeProvider().Select();
        }


        public RelayCommand OpenAddCargoTypeWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddCargoTypeWindow();
                    window.ShowDialog();
                    //刷新数据
                    CargoTypes = new CargoTypeProvider().Select();
                });
            }
        }


        public RelayCommand<object> DeleteCargoTypeCommand
        {
            get
            {
                return new RelayCommand<Object>((arg) =>
                {
                    if (!(arg is CargoType model)) return;
                    var cargo = new CargoProvider().Select().FindAll(t => t.TypeId == model.Id);
                    if (cargo != null && cargo.Count > 0)
                    {
                        MessageBox.Show("当前类型已有引用");
                        return;
                    }

                    var count = new CargoTypeProvider().Delete(model);
                    if(count > 0)
                    {
                        AppData.Instance.MainWindow.ShowMessageAsync("提示","删除完成");
                    }
                    //刷新数据
                    CargoTypes = new CargoTypeProvider().Select();
                });
            }
        }

    }//public class CargoTypeViewModel : ViewModelBase
}
